package org.lanzier.common.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author lansb
 *正则表达式验证
 */
public class RegexUtils {
	public static Map<String, String> regexMap = null;
	public static void init() throws IOException {
		regexMap = new HashMap<String, String>();
		ClassLoader cl = Environment.class.getClassLoader();
		InputStream is = cl
				.getResourceAsStream("/WEB-INF/conf/regex.properties");
		Properties props = new Properties();
		props.load(is);
		for(Entry entry : props.entrySet()) {
			regexMap.put(entry.getKey().toString(), entry.getValue().toString());
		}
	}
	public static boolean match(String regexName, String value) throws Exception {
		if(regexMap == null) init();
		Pattern pattern = Pattern.compile(regexMap.get(regexName));
		Matcher matcher = pattern.matcher(value);
		return matcher.matches();
	}
}
